package com.example.app;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.example.app.fragment.CartFragment;
import com.example.app.fragment.CatalogFragment;
import com.example.app.fragment.HomeFragment;
import com.example.app.fragment.MineFragment;

import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 默认加载首页页面
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fl_content, new HomeFragment())
                .commit();
        // 绑定 BottomNavigationView 并设置监听器
        BottomNavigationView navView = findViewById(R.id.menu_list);
        navView.setOnItemSelectedListener(navListener);
    }

    private final BottomNavigationView.OnNavigationItemSelectedListener navListener =
            item -> {
                Fragment selectedFragment = null;
                int itemId = item.getItemId();

                if (itemId == R.id.menu_home) {
                    selectedFragment = new HomeFragment();
                } else if (itemId == R.id.menu_catalog) {
                    selectedFragment = new CatalogFragment();
                } else if (itemId == R.id.menu_cart) {
                    selectedFragment = new CartFragment();
                } else if (itemId == R.id.menu_mine) {
                    selectedFragment = new MineFragment();
                }

                // 替换 Fragment
                if (selectedFragment != null) {
                    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.fl_content, selectedFragment)
                            .commit();
                }
                return true;
            };
}